package com.leigang.java.code.design.mode.proxy.dynamic;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author leig-a
 * @version 1.0
 * @desc
 * @date 2022/6/16 17:27
 */
public class CarSystemProxy {
    Car car;

    public Object getProxy() {
        if (car == null) {
            car = new Car();
        }
        return Proxy.newProxyInstance(getClass().getClassLoader(), car.getClass().getInterfaces(), new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                System.out.println("find proxy.... ");
                return method.invoke(car, args);
            }
        });
    }
}
